
package simax.integrator.test;

import org.codehaus.groovy.grails.web.converters.configuration.*
import org.codehaus.groovy.grails.web.converters.marshaller.json.*
import org.junit.*
import grails.converters.*

import simax.integrator.adapter.ExtModelAdapter;
import simax.integrator.model.RvMessage

import grails.test.mixin.web.*

class ExtModelAdapterTest {
	@Before
	void setup() {
		// Enable JSON Converter
		DefaultConverterConfiguration<JSON> defaultConverterConfig = new  DefaultConverterConfiguration<JSON>()
		defaultConverterConfig.registerObjectMarshaller(new CollectionMarshaller())
		defaultConverterConfig.registerObjectMarshaller(new MapMarshaller())
		defaultConverterConfig.registerObjectMarshaller(new GenericJavaBeanMarshaller())
		ConvertersConfigurationHolder.setTheadLocalConverterConfiguration(JSON.class, defaultConverterConfig);
	}
	
	@Test
	public void test() {
		
		def msg = new RvMessage(subject:'TEST', command:'TKIN')	
		def msgExpected = [
				dataModel: [
					extend:'Ext.data.Model', 
					fields:[
						[name:'header', type:'string'], 
						[name:'time', type:'date'], 
						[name:'subject', type:'string'], 
						[name:'msgbody', type:'string'], 
						[name:'command', type:'string']
					]
				], columns: [
					[header:'Header', dataIndex:'header'],
					[header:'Time', dataIndex:'time'],
					[header:'Subject', dataIndex:'subject'],
					[header:'Msgbody', dataIndex:'msgbody'],
					[header:'Command', dataIndex:'command']
				]
			] as JSON
		
		print msgExpected.toString()
		assertEquals(msgExpected.toString(), ExtModelAdapter.convert(msg).toString())
	}
}
